package com.maxatime.controller;

import static com.maxatime.utils.randomOrder.ArrayUtils.shuffle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.bgs.excel.ExcelReader;
//import com.bgs.excel.ExcelWriter;
import com.maxatime.pojo.Areas;
import com.maxatime.pojo.Cities;
import com.maxatime.pojo.Provinces;
import com.maxatime.pojo.studentInformation;
import com.maxatime.pojo.vo.studentInformationVO;
import com.maxatime.service.ERegisterService;
import com.maxatime.service.StudenService;
import com.maxatime.utils.MyContent;


@Controller
//@Slf4j
// @Log4j2
public class StudentController {

	Logger log = LogManager.getLogger();


    @Autowired
    private StudenService studenService;

    //学生注册
    @Autowired
    private ERegisterService eRegisterService;

    @RequestMapping("/we")
    public String we(){
        return "welcome1";
    }


    @RequestMapping("/li")
    public String lo(){
        return "stulist";
    }




//    //全查1
//    @RequestMapping("/StudentfindAll")
//    public String StudentfindAll(Model model){
//        List<studentInformation> stu = studenService.findAll();
//
//        model.addAttribute("stu",stu);
//
//        return "stulist1";
//    }

    //全查2-json
    @RequestMapping("/StudentfindAll1")
    @ResponseBody
    public  Map<String,Object> StudentfindAll1(){

        Map<String,Object> m = new HashMap<>();


        String sJson ="";

        List<studentInformation> stu = studenService.findAllqc();
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map

        log.info("List<studentInformation>:::"+stu);

        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,stu.size());
        m.put(MyContent.DATA,stu);


        return m;
    }

    //模糊差-json
    @RequestMapping("/StudentfindAll")
    @ResponseBody
    public  Map<String,Object> StudentfindAll(@RequestBody studentInformationVO studentInformationVO){

        //模糊查询前端传过来的数据
        log.info("模糊查询前端传过来的数据studentInformationVO"+studentInformationVO);

        Map<String,Object> m = new HashMap<>();

        studentInformation studentInformation = new studentInformation();

        //将VO的属性一个一个set给stu
        BeanUtils.copyProperties(studentInformationVO,studentInformation);
        log.info("VO传给的studentInformation"+studentInformation);

        String sJson ="";

        List<studentInformation> stu = studenService.findAll(studentInformation);
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map

        log.info("List<studentInformation>:::"+stu);

        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,stu.size());
        m.put(MyContent.DATA,stu);


        return m;
    }



    //全查+模糊差------新技术（因为需要返回map集合，但是呢，不推荐用map）
    @RequestMapping("/StudentfindAllormodil")
    @ResponseBody
    public  Map<String,Object> StudentfindAllormodil(@RequestBody studentInformationVO sVO){

        //模糊查询前端传过来的数据
        log.info("模糊查询前端传过来的数据studentInformationVO"+sVO);
        Map<String,Object> m = new HashMap<>();
        //设置分页信息(当前页码，每页显示数量)
        PageHelper.startPage(sVO.getPage(),sVO.getLimit());
        //调用分页查询账单类列表的方法
        List<studentInformation> slist = studenService.findAll(sVO);
        //创建分页对象
        PageInfo<studentInformation> pageInfo = new PageInfo<studentInformation>(slist);
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map
        log.info("List<studentInformation>:::"+pageInfo);
        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,pageInfo.getTotal());
        m.put(MyContent.DATA,pageInfo.getList());


        return m;
    }

    //全查+模糊差------新技术（因为需要返回map集合，但是呢，不推荐用map）
//    @RequestMapping("/StudentfindAllormodil1")
//    @ResponseBody
//    public DataGridViewResult StudentfindAllormodil1(@RequestBody studentInformationVO sVO){
//        //设置分页信息(当前页码，每页显示数量)
//        PageHelper.startPage(sVO.getPage(),sVO.getLimit());
//        //调用分页查询账单类列表的方法
//        List<studentInformation> slist = studenService.findAll(sVO);
//        //创建分页对象
//        PageInfo<studentInformation> pageInfo = new PageInfo<studentInformation>(slist);
//        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
//    }




    @RequestMapping("/addStu")
    @ResponseBody
    public Map<String,Object> addStu(@RequestBody studentInformation studentInformation){
        log.info("添加的"+studentInformation);
        Map<String,Object> m = new HashMap<String,Object>();
        if(studenService.insertSelective(studentInformation) > 0){
            m.put("success",true);
            m.put("message","添加成功");
        }else{
            m.put("success",false);
            m.put("message","添加失败");
        }
        return m;
    }

    //修改
    @RequestMapping("/updStu")
    @ResponseBody
    public Map<String,Object> updStu(@RequestBody studentInformation stu){
        Map<String,Object> m = new HashMap<>();
        log.info("修改的VO"+stu);
        if(studenService.updateByPrimaryKeySelective(stu) > 0){
            m.put("success",true);
            m.put("message","修改成功");
        }else{
            m.put("success",false);
            m.put("message","修改 失败");
        }
        return m;
    }



    //查询添加与修改的省下拉框
    @ResponseBody
    @RequestMapping("/provinces")
    public List<Provinces> provinces(){
        List<Provinces> plist = studenService.provinces();
        log.info("pppppppppppppppp"+plist);
        return plist;
    }

    //查询添加与修改的市下拉框
    @ResponseBody
    @RequestMapping("/cities")
    public List<Cities> cities(){
        List<Cities> clist = studenService.cities();

        log.info("ccccccccc"+clist);

        return clist;
    }

    //查询添加与修改的县/区下拉框
    @ResponseBody
    @RequestMapping("/areas")
    public List<Areas> areas(){
        List<Areas> alist = studenService.areas();
        log.info("aaaaaaaaa"+alist);
        return alist;
    }


    /**
     * 删除学生
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public Map<String, Object> deleteById(@RequestBody Integer stuId){
        Map<String,Object> m = new HashMap<>();
        System.out.println(stuId);
        if(studenService.deleteById(stuId)>0){
            m.put("success",true);
            m.put("message","删除成功");
        }else{
            m.put("success",false);
            m.put("message","删除失败");
        }
        return m;
    }

    /**
     * 批量删除账单
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String, Object> batchDelete(@RequestBody String ids){
        Map<String,Object> map = new HashMap<>();
        int count = 0;
        //将字符串拆分成数组
//        String[] idsStr = ids.split(",");
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray arrId = jsonObject.getJSONArray("ids");
        for (int i = 0; i < arrId.size(); i++) {
            System.out.printf("dsdss"+arrId.getInteger(i));
            log.info("批量删除"+arrId.getInteger(i));

            count = studenService.deleteById(arrId.getInteger(i));
            if(count>0){
                map.put("success",true);
                map.put("message","删除成功");
            }
        }
        //判断受影响行数是否为0
        if(count<=0){
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }


    @RequestMapping("/randomOrder")
    @ResponseBody
    public String[] randomOrder (@RequestBody String nams){

        //将字符串拆分成数组
        String[] names = nams.split(",");
        shuffle(names);

        return names;
    }


//    @RequestMapping("/export")
//    @ResponseBody
//    public String export(@RequestBody studentInformationVO sVO) {
//        // 创建需要写入的数据列表
////        List<StudentMessage> dataVOList = new ArrayList<>(2);
//        String exportFilePath = "D:\\guokai\\phoee.xlsx";
//        List<studentInformation> dataVOList =   studenService.stuexport(sVO);
//
//        System.out.println("studenService.stuexport()::::");
//
//        // 写入数据到工作簿对象内
//        Workbook workbook = ExcelWriter.exportData(dataVOList);
//
//        // 以文件的形式输出工作簿对象
//        FileOutputStream fileOut = null;
//        try {
//
//            File exportFile = new File(exportFilePath);
//            if (!exportFile.exists()) {
//                exportFile.createNewFile();
//            }
//
//            fileOut = new FileOutputStream(exportFilePath);
//            workbook.write(fileOut);
//            fileOut.flush();
//        } catch (Exception e) {
//            log.info("输出Excel时发生错误，错误原因：" + e.getMessage());
//        } finally {
//            try {
//                if (null != fileOut) {
//                    fileOut.close();
//                }
//                if (null != workbook) {
//                    workbook.close();
//                }
//            } catch (IOException e) {
//                log.info("关闭输出流时发生错误，错误原因：" + e.getMessage());
//            }
//        }
//        return "stulist";
//
//    }
//
//
//    @RequestMapping("/import")
//    public String importss(){
//
//        String excelFileName = "D:\\guokai\\pho.xlsx";
//        // 读取Excel文件内容
//        List<studentInformation> readResult = ExcelReader.readExcel(excelFileName);
//
//        System.out.println("studenService.inse(readResult):::");
//        studenService.inse(readResult);
//        eRegisterService.inse(readResult);
//
//        return "stulist";
//    }






}
